package cn.edu.lingnan.part.controller;

import cn.edu.lingnan.part.entity.Student;
import cn.edu.lingnan.part.entity.common.CommonResult;
import cn.edu.lingnan.part.listener.StudentExcelListener;
import cn.edu.lingnan.part.service.StudentService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2020-05-31 17:16:07
 */
@Controller
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Student selectOne(Integer id) {
        return this.studentService.queryById(id);
    }


    @RequestMapping("toList")
    public String toList(){
        return "user/student_list";
    }


    /**
     * 查询全部
     * @param page
     * @param limit
     * @param student
     * @return
     */

    @PostMapping("queryAll")//这里也是提交一个表单申请，所以也是用post请求
    @ResponseBody//返回的是数据
    public Object queryAll(Integer page, Integer limit, Student student){

        System.out.println(student);
        CommonResult<Student> result = new CommonResult<>();
        IPage<Student> iPage = studentService.queryAllByLimit(page,limit,student);
        result.setCode(0);
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }


    /**
     * 去到添加页面
     * @param model
     * @return
     */
    @GetMapping("/")
    public String toAdd(Model model){
        model.addAttribute("student",new Student());
        return "user/student_add";
    }
    /**
     * 去到修改页面
     * @param model
     * @return
     */
    @GetMapping("toEdit")
    public String toEdit(Integer id, Model model){
        System.out.println("id------"+ id);
        Student student = studentService.queryById(id);
        //将调出来页面设置为student修改的页面
        model.addAttribute("student", student);
        return "user/student_add";
    }


    /**
     * 添加学生,因为添加编辑用的是同一个页面，所以需要操作进行一个判断
     * 判断当前的操作是添加还是编辑
     * @param student
     *
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Object save(Student student) {
        boolean result = false;
        //有一个判断逻辑需要处理，就是判断添加的账号是否已存在
//        Student student1 = studentService.queryByAccount(student.getAccount());
        if (student.getId() != null) {
//      编辑

//      model.addAttribute("msg", "该账号已经存在");
            result = studentService.update(student) > 0;
        } else {
//            添加
//            //设置注册的时间，调用系统时间
//            student.setRegData(new Date());
            //做添加是不会有id的
            result = studentService.insert(student).getId() != null;
        }
        return result;
    }

    //    @DeleteMapping("deleteById") 先使用post请求
    @PostMapping("/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids){
//        System.out.println(ids);
        if (ids == null || ids.length == 0){
            return false;
        }
//        System.out.println(Arrays.toString(ids));
        studentService.deleteById(Arrays.asList(ids));
        return true;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ResponseBody
    public Object upload(MultipartFile file) {
        CommonResult<String> result = new CommonResult<>();
        try {
            EasyExcel.read(file.getInputStream(), Student.class, new StudentExcelListener(studentService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg("Excel上传出错");
        }
        result.setData(file.getOriginalFilename());
        return result;
    }




}